package com.zjkj.mp.controller.messageManage;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zjkj.mp.service.message.TemplateManagerService;
import com.zjkj.mp.service.user.UserService;
import com.zjkj.mp.utils.PaginatedResult;
import com.zjkj.mp.utils.security.SpringSecurityUtils;
import com.zjkj.mp.vo.messageManager.Template;
import com.zjkj.mp.vo.messageManager.TemplateCategory;
import com.zjkj.mp.vo.user.MpUser;

/**
 * 
 * 短信模板控制器
 *
 * @author 陈晨
 * @history
 * <TABLE id="HistoryTable" border="1">
 * 	<TR><TD>时间</TD><TD>描述</TD><TD>作者</TD></TR>
 *	<TR><TD>2013-6-9</TD><TD>创建初始版本</TD><TD>陈晨</TD></TR>
 * </TABLE>
 */
@Controller
public class DXMBController {

	@Resource(name = "userServiceImpl")
	private UserService userService;
	@Autowired
	private TemplateManagerService templateManagerService;
	private static Log log = LogFactory.getLog(DXMBController.class);

	/******************************* 新建短信 ***********************************/

	/**
	 * 跳转至短信模板页面
	 * @return
	 */
	@RequestMapping("/addThemeMessage.do")
	public String themeMessage() {
		return "message/addThemeMessage";
	}
	/**
	 * 查询分类
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/queryCategory.do")
	public String queryCategory(HttpServletRequest request,HttpServletResponse response){
		try {
			String current = request.getParameter("current");
			int currentPage = 1;
			int nextPage = 0;
			int proPage = 0;
			if(current != null && current != ""){
				currentPage = Integer.parseInt(current);
			}
			Map map = new HashMap();
			int userId = ((MpUser)(SpringSecurityUtils.getCurrentUser())).getUserId();
			String title = request.getParameter("Category");
			map.put("userId", userId);
			map.put("tempTitle", title);
			map.put("currentPage", currentPage);
			PaginatedResult result = templateManagerService.findQueryResult(map);
			nextPage = currentPage +1;
			proPage = currentPage -1;
			if(nextPage >= result.getTotalSize()){
				nextPage = result.getTotalSize();
			}
			if(proPage <= 0){
				proPage = 1;
			}
			request.setAttribute("currentPage", currentPage);
			request.setAttribute("nextPage", nextPage);
			request.setAttribute("proPage", proPage);
			request.setAttribute("pageCount", result.getPageCount());
			request.setAttribute("resultList", result.getResult());
			request.setAttribute("tempTitle", title);
		} catch (Exception e) {
			log.error("获取查询结果异常", e);
		}
		return "message/themeMessage";
		
	}
	
	/**
	 * 显示全部分类
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/showAllCategory.do")
	public String showAllCategory(HttpServletRequest request,HttpServletResponse response){
		//获取当前用户下的所有的类别信息
		try {
			int userId = ((MpUser)(SpringSecurityUtils.getCurrentUser())).getUserId();
			String current = request.getParameter("current");
			int currentPage = 1;
			int nextPage = 0;
			int proPage = 0;
			if(current != null && current != ""){
				currentPage = Integer.parseInt(current);
			}
			Map map = new HashMap();
			String title = request.getParameter("Category");
			map.put("userId", userId);
			map.put("currentPage", currentPage);
			PaginatedResult result = templateManagerService.findAllQueryResult(map);
			nextPage = currentPage +1;
			proPage = currentPage -1;
			if(nextPage >= result.getTotalSize()){
				nextPage = result.getTotalSize();
			}
			if(proPage <= 0){
				proPage = 1;
			}
			request.setAttribute("currentPage", currentPage);
			request.setAttribute("nextPage", nextPage);
			request.setAttribute("proPage", proPage);
			request.setAttribute("pageCount", result.getPageCount());
			request.setAttribute("resultList", result.getResult());
		}catch (Exception e) {
			log.error("获取全部结果异常", e);
		}
		return "message/themeMessage";
		
	}
	/**
	 * 添加分类
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/doaddCategory.do")
	public String doaddCategory(HttpServletRequest request,HttpServletResponse response){
		
		try {
			TemplateCategory category = new TemplateCategory();
			//获取创建人ID
			int userId = ((MpUser)(SpringSecurityUtils.getCurrentUser())).getUserId();
			String title = request.getParameter("addCategory");
			category.setUserId(userId);
			category.setTempTitle(title);
			category.setCreatetime(new Date());
			if(templateManagerService.updateTemplateCat(category)== 0){
				templateManagerService.createTemplateCat(category);
			}
		} catch (Exception e) {
			log.error("添加模板分类出现异常", e);
		}
		return "message/themeMessage";
		
	}
	
	/**
	 * 更新分类代码操作
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/doUpdateTemplateCat.do")
	public String doUpdateTemplateCat(HttpServletRequest request,HttpServletResponse response){
		try {
			Map map = new HashMap();
			int userId = ((MpUser)(SpringSecurityUtils.getCurrentUser())).getUserId();
			String tempID = request.getParameter("bz");
			String title = request.getParameter(tempID);
			map.put("userId", userId);
			map.put("tempID", tempID);
			map.put("title", title);
			templateManagerService.updateTemplateCat(map);
			
		} catch (Exception e) {
			log.error(" 更新分类代码操作异常", e);
		}
		return "message/themeMessage";
	}

	/**
	 * 删除分类操作
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/deleteNewTemplateCat.do")
	public String deleteNewTemplateCat(HttpServletRequest request,HttpServletResponse response){
		try {
			Map map = new HashMap();
			int userId = ((MpUser)(SpringSecurityUtils.getCurrentUser())).getUserId();
			String tempId = request.getParameter("tempId");
			map.put("userId", userId);
			map.put("tempId", tempId);
			templateManagerService.deleteTempCat(map);
		} catch (Exception e) {
			log.error("删除分类操作异常", e);
		}
		return "message/themeMessage";
		
	}
	
	/**
	 * 编辑增加模板
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/editTemplateCat.do")
	public String editTemplateCat(HttpServletRequest request,HttpServletResponse response){
		try {
			Map map = new HashMap();
			Template template = null;
			int userId = ((MpUser)(SpringSecurityUtils.getCurrentUser())).getUserId();
			String tempId = request.getParameter("tempId");
			String templateId = request.getParameter("templateId");
			map.put("userId", userId);
			map.put("tempId", tempId);
			map.put("templateId", templateId);
			List resultList = templateManagerService.findAllTemplateTyle(map);
			template = templateManagerService.findTemplate(map);
			if(template != null){
				request.setAttribute("template", template);	
			}else{
				request.setAttribute("templateId", templateId);
			}
			request.setAttribute("resultList", resultList);
			request.setAttribute("tempId", tempId);
		} catch (Exception e) {
			log.error("编辑增加模板异常", e);
		}
		return "message/newTemplate";
		
	}
	
	/**
	 * 创建模板
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/addTemplateCat.do")
	public String addTemplateCat(HttpServletRequest request,HttpServletResponse response){
		try {
			Map map = new HashMap();
			int userId = ((MpUser)(SpringSecurityUtils.getCurrentUser())).getUserId();
			String teplate = request.getParameter("template");
			String temId = request.getParameter("dropdown");
			String templateId = request.getParameter("tempId");
			if(temId != null){
				int tempId = Integer.parseInt(temId);
				map.put("tempId", tempId);
			}
			String tag = request.getParameter("tag");
			map.put("userId", userId);
			map.put("createTime", new Date());
			map.put("nr", teplate);
			map.put("tag", tag);
			map.put("templateId", templateId);
			if(templateManagerService.updateTemplate(map) == 0){
				templateManagerService.createTemplate(map);	
			}
		}catch (Exception e) {
			log.error("创建模板异常", e);
		}
		return "message/themeMessage";
		
	}

	/**
	 * 获取所有模板
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/showAllTemplate.do")
	public String showAllTemplate(HttpServletRequest request,HttpServletResponse response){
		try {
			Map map = new HashMap();
			int userId = ((MpUser)(SpringSecurityUtils.getCurrentUser())).getUserId();
			String current = request.getParameter("current");
			map.put("userId", userId);
			int currentPage = 1;
			int nextPage = 0;
			int proPage = 0;
			if(current != null){
				currentPage = Integer.parseInt(current);
			}
			map.put("current", currentPage);
			PaginatedResult result = templateManagerService.findAllTemplateList(map);
			nextPage = currentPage +1;
			proPage = currentPage -1;
			if(nextPage >= result.getTotalSize()){
				nextPage = result.getTotalSize();
			}
			if(proPage <= 0){
				proPage = 1;
			}
			request.setAttribute("currentPage", currentPage);
			request.setAttribute("nextPage", nextPage);
			request.setAttribute("proPage", proPage);
			request.setAttribute("pageCount", result.getPageCount());
			request.setAttribute("resultList", result.getResult());
		}catch (Exception e) {
			log.error("获取所有模板异常", e);
		}
		return "message/showTemplateList";
		
	}
	/**
	 * 根据tag查询
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/querytemplate.do")
	public String querytemplate(HttpServletRequest request,HttpServletResponse response){
		try {
			Map map = new HashMap();
			int userId = ((MpUser)(SpringSecurityUtils.getCurrentUser())).getUserId();
			String current = request.getParameter("page");
			String tag = request.getParameter("tag");
			map.put("userId", userId);
			int currentPage = 1;
			int nextPage = 0;
			int proPage = 0;
			if(current != null){
				currentPage = Integer.parseInt(current);
			}
			map.put("currentPage", currentPage);
			map.put("tag", tag);
			PaginatedResult result = templateManagerService.findQueryTemplateList(map);
			nextPage = currentPage +1;
			proPage = currentPage -1;
			if(nextPage >= result.getTotalSize()){
				nextPage = result.getTotalSize();
			}
			if(proPage <= 0){
				proPage = 1;
			}
			request.setAttribute("currentPage", currentPage);
			request.setAttribute("nextPage", nextPage);
			request.setAttribute("proPage", proPage);
			request.setAttribute("tag", tag);
			if(result != null){	
			request.setAttribute("pageCount", result.getPageCount());
			request.setAttribute("resultList", result.getResult());
			}
		}catch (Exception e) {
			log.error("获取所有模板异常", e);
		}
		return "message/showQueryResult";
		
	}
	
}
